﻿using Cookbook.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel.DataTransfer;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Cookbook.Views
{
  /// <summary>
  /// An empty page that can be used on its own or navigated to within a Frame.
  /// </summary>
  public sealed partial class RecipeDetailView : Page
  {
    private RecipeDetailViewModel viewModel;

    public RecipeDetailView()
    {
      this.InitializeComponent();
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
      base.OnNavigatedTo(e);

      var id = (int)e.Parameter;

      this.viewModel = new RecipeDetailViewModel();
      this.DataContext = this.viewModel;

      await this.viewModel.LoadRecipeDetail(id);

      var shareManager = DataTransferManager.GetForCurrentView();
      shareManager.DataRequested += shareManager_DataRequested;
    }

    void shareManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
      var recipe = viewModel.Recipe;

      args.Request.Data.Properties.Title = recipe.Title;
      args.Request.Data.Properties.Description = recipe.Instructions;

      string html = @"<div>
                          <h2>{0}</h2>
                          <img src='{2}' /><br/>
                          <p>{1}</p>
                      </div>";

      html = string.Format(html,
          recipe.Title,
          recipe.Instructions,
          recipe.ImagePath);

      args.Request.Data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(html));
      args.Request.Data.SetText(recipe.Title + "\n" + recipe.Instructions);
    }
  }
}
